<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="icon" th:href="@{/img/logo.ico}" href="../static/img/logo.ico" type="images/x-ico"/>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}">
    <title>已选课程</title>
    <link rel="stylesheet" th:href="@{/fontawesome/css/font-awesome.min.css}"
          href="../../static/fontawesome/css/font-awesome.min.css">

    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,500;0,600;0,700;1,400&display=swap">

    <!-- Fontawesome CSS -->
    <link rel="stylesheet" href="../static/fontawesome/css/fontawesome.min.css"
          th:href="@{/fontawesome/css/fontawesome.min.css}">
    <link rel="stylesheet" href="../static/fontawesome/css/all.min.css" th:href="@{/fontawesome/css/all.min.css}">

    <!-- Main CSS -->
    <link rel="stylesheet" href="../static/css/back_index.css" th:href="@{/css/back_index.css}">
</head>
<body>
<div class="main-wrapper">
    <div th:replace="commons/bar::#studentmenubar(activeUri1='已选课程')"></div>
    <div class="page-wrapper">
        <div class="content container-fluid">
            <table class="table table-striped table-bordered" id="datas">
                <thead>
                <tr>
                    <th scope="col">名称</th>
                    <th scope="col">类型</th>
                    <th scope="col">学时</th>
                    <th scope="col">学分</th>
                    <th scope="col">操作</th>
                </tr>
                </thead>
                <tbody>
                <tr id="template">
                    <td class="courseName"></td>
                    <td class="courseType"></td>
                    <td class="time"></td>
                    <td class="credits"></td>
                    <td class="operation"></td>
                </tr>
                </tbody>
            </table>
            <div class="modal fade" id="viewCourseModal" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">课程详情</h4>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <table align="center" class="table table-bordered">
                                <tr>
                                    <td>名称</td>
                                    <td>
                                        <span id="courseName"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>ID</td>
                                    <td>
                                        <span id="courseId"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>类型</td>
                                    <td>
                                        <span id="courseType"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>学时</td>
                                    <td>

                                        <span id="time"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>学分</td>
                                    <td>
                                        <span id="credits"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>开始选报时间</td>
                                    <td>
                                        <span id="beginDate"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>结束选报时间</td>
                                    <td>
                                        <span id="endDate"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>描述</td>
                                    <td>
                                        <span id="info"></span>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary" id="submitUnSelect">退选</button>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">我已了解</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Footer -->
            <footer>
                <p>2020 &copy; 软件学院学生信息管理系统 by 郑焙元</p>
            </footer>
            <!-- /Footer -->
        </div>
    </div>
</div>
<script src="../static/js/jquery-3.5.1.min.js" th:src="@{/js/jquery-3.5.1.min.js}"></script>
<script>

    $(function () {
        TableInit();
        var buttonGroup = new ButtonGroup();
        buttonGroup.init();
    });

    var TableInit = function () {
        sendAjax("/student/api/course", "get", {}, function (res) {
            $.each(res.data, function (i, course) {
                var row = $("#template").clone();
                row.find(".courseName").text(course.coursename);
                var type = course.coursetype === "7" ? "必修" : "选修";
                row.find(".courseType").text(type);
                row.find(".time").text(course.time);
                row.find(".credits").text(course.credits);
                let html = '<button type="button" class="btn btn-info btn-sm" data-toggle="modal" ' +
                    'data-target="#viewCourseModal" onclick="viewCourse(\'' + course.courseid + '\')">详情</button>';
                row.find(".operation").html(html);
                var dataid = 1;
                row.appendTo("#datas");//添加到模板的容器中
            });
            $("#template:first").remove();
        }, '');
    }


    function viewCourse(courseid) {
        sendAjax('/student/api/course/' + courseid, 'get', {}, function (res) {
            $("#courseName").text(res.data.coursename);
            $("#courseId").text(res.data.courseid);
            $("#courseType").text(switchCoursetype(res.data.coursetype));
            $("#time").text(res.data.time);
            $("#credits").text(res.data.credits);
            $("#beginDate").text(res.data.begindate.substring(0, 10));
            $("#endDate").text(res.data.enddate.substring(0, 10));
            $("#info").text(res.data.info);
        }, '');
    }

    var ButtonGroup = function () {
        var object = {};

        object.init = function () {
            $("#submitUnSelect").click(function () {
                if (confirm("确认退选该课程？")) {
                    sendAjax("/student/api/course/" + $("#courseId").text(), "delete", {}, function () {
                        alert("退选成功");
                        $("#viewCourseModal").modal('hide');
                    }, "#submitUnSelect");
                }
            })
        };

        return object;
    };

</script>
<script src="../static/js/popper.min.js" th:src="@{/js/popper.min.js}"></script>
<script src="../static/js/bootstrap.min.js" th:src="@{/js/bootstrap.min.js}"></script>

<!-- Slimscroll JS -->
<script src="../static/js/jquery.slimscroll.min.js" th:src="@{/js/jquery.slimscroll.min.js}"></script>

<!-- Custom JS -->
<script src="../static/js/script.js" th:src="@{/js/script.js}"></script>

</body>
</html>
